SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TCMOVI_CAJA_I07]    
/*-------------------------------------------------------*/    
/*---Empresa              : OFISIS S.A.                --*/    
/*---Cliente              : OFISIS S.A.                --*/    
/*---Sistema              : Tesoreria                  --*/    
/*---M=dulo               : Empresa                    --*/    
/*---Programa             : Liquidacion de Planillas   --*/    
/*---Script               : SP_TCMOVI_CAJA_I07.sql     --*/    
/*---Nombre SP            : SP_TCMOVI_CAJA_I07         --*/    
/*---Desarrollado por     : Carlos R Marquez Escobar   --*/    
/*---Fecha Creacion       : 02/06/2001                 --*/    
/*---Base Datos           : SQL Server         --*/    
/*---Version              : 7.00                       --*/    
/*---Invoca a SP          :                            --*/    
/*-------------------------------------------------------*/    
/*-------------------------------------------------------*/    
/*--- Modificado 1 por     : Denis Villacres P.        --*/    
/*--- Fecha Modificacion   : 15/06/2001                --*/     
/*--- Detalle Modificacion : Migracion SQL 7.00        --*/    
/*-------------------------------------------------------*/    
/*-------------------------------------------------------*/    
/*---Modificado 2 por     : Maribel Julca Rodriguez    --*/    
/*---Fecha Modificaci«n   : 21/04/2005                 --*/     
/*---Detalle Modificaci«n : ingreso de los campos CO_USUA_CREA, CO_USUA_MODI  --*/    
/*-------------------------------------------------------*/    
   
/*-------------------------------------------------------*/    
/*---Comentarios                                       --*/    
/*---                                                  --*/    
/*---                                                  --*/    
/*-------------------------------------------------------*/    
    
/*-------------------------------------------------------*/    
/*--- Drop Proc SP_TCMOVI_CAJA_I07                     --*/    
/*--- Grant All On SP_TCMOVI_CAJA_I07 To Public        --*/     
/*--- SP_TCMOVI_CAJA_I07 '99','OFISIS', 'SOL', 'DOL', '00000002','2001','06'        --*/    
/*-------------------------------------------------------*/    
@ISCO_EMPR         TD_VC_002,    
@ISCO_USUA       TD_VC_008_USUA,    
@ISCO_MONE_DEFA    TD_VC_003,    
@ISCO_MONE_EXTR    TD_VC_003,    
@ISCO_CAJA     TD_VC_008,    
@ISAA_MOVI     TD_VC_004,    
@ISMM_MOVI     TD_VC_002  
  
  
As  
Begin Tran SP_TCMOVI_CAJA_I07    
    
Declare    
    
@VSTI_CORR_INEG  TD_VC_001,    
@VSFR_CORR_INEG  TD_VC_001,    
@VNUL_CORR_INGR  TD_IN_001,    
@VNUL_CORR_INEG  TD_IN_001,    
@VNNU_COMP_MOVI  TD_IN_001,    
@VNIM_MOVI_AUXI  TD_NU_016_004,    
@VNIM_SALD_CONV  TD_NU_016_004,    
@VNIM_INGR   TD_NU_016_004,    
@VSST_SIGN   TD_VC_001,    
@VSNU_PLAN   TD_VC_010,    
@VSCO_BANC   TD_VC_003,    
@VSNU_CNTA_BANC  TD_VC_020,    
@VSCO_CAJA   TD_VC_008,    
@VSAA_MOVI   TD_VC_004,    
@VSMM_MOVI   TD_VC_002,    
@VSNU_COMP_MOVI  TD_VC_008,    
@VSDE_NOMB_ORDE  TD_VC_100,    
@OSTI_DOCU   TD_VC_003,    
@VNNU_SECU   TD_IN_001,    
    
@CSFL_DEST  TD_VC_001,    
@CSNO_AUXI   TD_VC_100,    
@CSCO_CAJA_DEST  TD_VC_008,    
@CSCO_BANC_DEST  TD_VC_003,    
@CSNU_CNTA_DEST  TD_VC_020,    
@CSCO_MONE_DEST  TD_VC_003,    
@CSAA_DEST  TD_VC_004,    
@CSMM_DEST  TD_VC_002,    
@CDFE_OPER_DEST  TD_DT_001,    
@CNFA_CAMB_DEST  TD_NU_015_006,    
@CNIM_MOVI_DEST  TD_NU_016_004,    
@CSCO_TIPO_DEST  TD_VC_003,    
@CSCO_UNID_DEST  TD_VC_003,    
@CSTI_ENTI_DEST  TD_VC_001,    
@CSCO_ENTI_DEST  TD_VC_020,    
@CSDE_OBSE_DEST  TD_VC_100,    
@CSTI_DOCU_DEST  TD_VC_003,    
@CSNU_DOCU_DEST  TD_VC_020,    
@CSCO_SUBR_DEST  TD_VC_003,    
    
--**********************    
    
@CSFL_DEST_CUR2  TD_VC_001,    
@CSNO_AUXI_CUR2   TD_VC_100,    
@CSCO_CAJA_CUR2  TD_VC_008,    
@CSCO_BANC_CUR2  TD_VC_003,    
@CSCO_BACH_CUR2  TD_VC_003,    
@CSNU_CNTA_CUR2  TD_VC_020,    
@CSCO_MONE_CUR2  TD_VC_003,    
@CSAA_DEST_CUR2  TD_VC_004,    
@CSMM_DEST_CUR2  TD_VC_002,    
@CDFE_OPER_CUR2  TD_DT_001,    
@CNFA_CAMB_CUR2  TD_NU_015_006,    
@CNIM_MOVI_CUR2  TD_NU_016_004,    
@CNIM_CONV_CUR2  TD_NU_016_004,    
@CSCO_TIPO_CUR2  TD_VC_003,    
@CSCO_UNID_CUR2  TD_VC_003,    
@CSTI_ENTI_CUR2  TD_VC_001,    
@CSCO_ENTI_CUR2  TD_VC_020,    
@CSDE_OBSE_CUR2  TD_VC_100,    
@CSTI_DOCU_CUR2  TD_VC_003,     
@CSNU_DOCU_CUR2  TD_VC_020,    
@VSST_ASTO_RESU  TD_VC_003    
    
    
    
--**********************    
    
Declare CU_TCMOVI_CAJA_I07_1 Cursor    
For    
 Select MAX(T5.FL_DEST),    
 Rtrim(Ltrim(T1.CO_BANC_DEST)),    
 Rtrim(Ltrim(T1.NU_CNTA_DEST)),    
 Rtrim(Ltrim(T1.TI_DOCU_DEST)),    
 Rtrim(Ltrim(T1.NU_DOCU_DEST)),    
 MAX(T1.CO_CAJA_DEST) ,    
 Max (T1.CO_MONE_DEST), Max (T1.FE_OPER_DEST),    
 T1.TI_DOCU_DEST , T1.NU_DOCU_DEST,    
 Max(T1.TI_ENTI_DEST) ,     
 Max(T1.CO_ENTI_DEST) ,    
 Max(T3.NO_AUXI),    
 Max(T1.CO_TIPO_DEST),    
 Max(T1.AA_DEST), Max(T1.MM_DEST), Max(T1.CO_UNID_DEST ),     
 Avg(T1.FA_CAMB_DEST) , Sum(T1.IM_MOVI_DEST ), Max(T1.DE_OBSE_DEST), Max(T1.CO_SUBR_DEST)      
 From  TCMOVI_CAJA T1 , TTMONE T2 , TMAUXI_EMPR T3, TMCNTA_BANC T4, #TWDOCU_CART_LIQU T5    
 Where T1.CO_EMPR = @ISCO_EMPR    
 And T1.CO_CAJA = @ISCO_CAJA     
 And T1.AA_CAJA  = @ISAA_MOVI    
 And T1.MM_CAJA  = @ISMM_MOVI    
 And T1.TI_MOVI = 'I'    
 And T1.TI_SITU = 'CAR'    
 And T1.NU_CNTA_DEST Is Not Null    
 And T1.CO_MONE_DEST = T2.CO_MONE    
 And T1.CO_EMPR = T3.CO_EMPR    
 And T1.TI_ENTI_DEST = T3.TI_AUXI_EMPR    
 And T1.CO_ENTI_DEST = T3.CO_AUXI_EMPR    
 And T1.CO_EMPR = T4.CO_EMPR    
 And T1.CO_BANC_DEST = T4.CO_BANC    
 And T1.NU_CNTA_DEST = T4.NU_CNTA_BANC    
 And T1.CO_BANC_DEST = T5.CO_BANC_DEST    
 And T1.NU_CNTA_DEST = T5.NU_CNTA_DEST    
 And T1.TI_DOCU_DEST = T5.TI_DOCU_DEST    
 And T1.NU_DOCU_DEST = T5.NU_DOCU_DEST      
 And T5.FL_DEST = 'T'    
 Group By T1.CO_BANC_DEST , T1.NU_CNTA_DEST , T1.TI_DOCU_DEST, T1.NU_DOCU_DEST    
 UNION ALL    
 Select Max(T5.FL_DEST),    
 Rtrim(Ltrim(IsNull(Max(T1.CO_BANC_DEST),'' ) )),    
 Rtrim(Ltrim(IsNull(Max(T1.NU_CNTA_DEST),'' ) )),    
 Rtrim(Ltrim(IsNull(Max(T1.TI_DOCU_DEST),'' ) )),    
 Rtrim(Ltrim(IsNull(Max(T1.CO_BANC_DEST),'' ) )),    
 T1.CO_CAJA_DEST ,     
 Max(T1.CO_MONE_DEST), Max(T1.FE_OPER_DEST),    
 Max(T1.TI_DOCU_DEST), Max(T1.NU_DOCU_DEST),    
 Max(T1.TI_ENTI_DEST), Max(T1.CO_ENTI_DEST),    
 Max(T3.NO_AUXI),    
 Max(T1.CO_TIPO_DEST),    
 Max(T1.AA_DEST), Max (T1.MM_DEST), Max(T1.CO_UNID_DEST ),     
 Avg(T1.FA_CAMB_DEST), Sum(T1.IM_MOVI_DEST), Max(T1.DE_OBSE_DEST), Max(T1.CO_SUBR_DEST)     
 From  TCMOVI_CAJA T1 , TTMONE T2 , TMAUXI_EMPR T3, TMCAJA T4, #TWDOCU_CART_LIQU T5    
 Where T1.CO_EMPR = @ISCO_EMPR    
 And T1.CO_CAJA = @ISCO_CAJA     
 And T1.AA_CAJA  = @ISAA_MOVI    
 And T1.MM_CAJA  = @ISMM_MOVI    
 And T1.TI_MOVI = 'I'    
 And T1.TI_SITU = 'CAR'    
 And T1.CO_CAJA_DEST Is Not Null    
 And T1.CO_MONE_DEST = T2.CO_MONE    
 And T1.CO_EMPR = T3.CO_EMPR    
 And T1.TI_ENTI_DEST = T3.TI_AUXI_EMPR    
 And T1.CO_ENTI_DEST = T3.CO_AUXI_EMPR    
 And T1.CO_EMPR = T4.CO_EMPR    
 And T1.CO_CAJA_DEST = T4.CO_CAJA    
 And T1.CO_CAJA_DEST = T5.CO_CAJA_DEST    
 And T5.FL_DEST = 'C'    
 Group By T1.CO_CAJA_DEST     
 Order by 1    
    
    
Begin    
Open  CU_TCMOVI_CAJA_I07_1    
Fetch  CU_TCMOVI_CAJA_I07_1 Into     
 @CSFL_DEST,    
 @CSCO_BANC_DEST,    
 @CSNU_CNTA_DEST,    
 @CSTI_DOCU_DEST,    
 @CSNU_DOCU_DEST,    
 @CSCO_CAJA_DEST ,    
 @CSCO_MONE_DEST , @CDFE_OPER_DEST,    
 @CSTI_DOCU_DEST , @CSNU_DOCU_DEST,    
 @CSTI_ENTI_DEST ,     
 @CSCO_ENTI_DEST ,    
 @CSNO_AUXI,    
 @CSCO_TIPO_DEST,    
 @CSAA_DEST  , @CSMM_DEST, @CSCO_UNID_DEST,    
 @CNFA_CAMB_DEST  , @CNIM_MOVI_DEST, @CSDE_OBSE_DEST , @CSCO_SUBR_DEST     
    
While (@@FETCH_STATUS = 0)    
 BEGIN --LOOP    
 If (@@FETCH_STATUS < 0)    
     Begin    
     RAISERROR 20001 'FIN DE ARCHIVO'    
     CLOSE CU_TCMOVI_CAJA_I07_1    
     DEALLOCATE CU_TCMOVI_CAJA_I07_1    
     RETURN    
      End    
    
 Select @VSST_ASTO_RESU = T1.ST_ASTO_RESU    
 From TMOPRC_RESU T1, TDTIPO_OPER T2    
 Where T1.CO_EMPR = T2.CO_EMPR    
 And T2.CO_TIPO_OPER = @CSCO_TIPO_DEST    
 And T1.CO_OPRC_CNTB = T2.CO_OPRC_CNTB    
    
 If @CSFL_DEST = 'T'  ---Bancos    
    BEGIN    
     Select  @VNNU_COMP_MOVI = Isnull(Max(Convert(Numeric,NU_COMP_BANC)),0)    
     From  TCMOVI_BANC    
     Where  CO_EMPR = @ISCO_EMPR     
     And CO_BANC = @CSCO_BANC_DEST     
     And NU_CNTA_BANC = @CSNU_CNTA_DEST    
     And AA_BNCO = @CSAA_DEST    
     And MM_BNCO = @CSMM_DEST     
     And TI_MOVI_BANC = 'I'    
    
     Select  @VSTI_CORR_INEG = TI_CORR_INEG     
--    @VSFR_CORR_INEG = FR_CORR_INEG,     
--    @VNUL_CORR_INGR = UL_CORR_INGR    
     From  TMCNTA_BANC    
     Where CO_EMPR = @ISCO_EMPR    
    And Rtrim(CO_BANC) = Rtrim( @CSCO_BANC_DEST )    
    And Rtrim(NU_CNTA_BANC) = Rtrim( @CSNU_CNTA_DEST )    
    
     Select @VNNU_COMP_MOVI = @VNNU_COMP_MOVI + 1    
     Select @VSNU_COMP_MOVI = Replicate('0',8-LEN(Convert(Varchar,@VNNU_COMP_MOVI))) + Convert(Varchar,@VNNU_COMP_MOVI)     
     
    
  -- INSERTA CABECERA BANCOS    
  Begin     
    
  Exec SP_TCMOVI_BANC_I04 @ISCO_EMPR, @ISCO_USUA,     
   @CSCO_BANC_DEST, @CSNU_CNTA_DEST , @CSAA_DEST , @CSMM_DEST , 'I',     
   @CDFE_OPER_DEST, @CSCO_TIPO_DEST, @CSCO_SUBR_DEST , @CSTI_DOCU_DEST, @CSNU_DOCU_DEST,     
   @CNFA_CAMB_DEST, @CSCO_BANC_DEST, @CSNU_DOCU_DEST, @CNIM_MOVI_DEST, @CSTI_ENTI_DEST, @CSCO_ENTI_DEST,     
   @CSNO_AUXI, 'S' , @CSDE_OBSE_DEST, @VSTI_CORR_INEG, @VNUL_CORR_INGR,    
   @VNUL_CORR_INEG, @CDFE_OPER_DEST, NULL, @CSCO_UNID_DEST, NULL, NULL,     
   NULL, NULL, NULL, @VSST_ASTO_RESU, @VSNU_COMP_MOVI OUTPUT     
    
    
     If @@error <> 0    
      Begin    
       Rollback tran    
       Close CU_TCMOVI_CAJA_I07_1    
       DEALLOCATE CU_TCMOVI_CAJA_I07_1    
       Return    
      End    
    
  End -- FIN INSERTA CABECERA BANCOS    
    
    
--********************** cursor 2    
    
SELECT @VNNU_SECU = 0    
    
Declare CU_TCMOVI_CAJA_I07_2 Cursor    
For    
 Select T5.FL_DEST,    
 Rtrim(Ltrim(T1.CO_BANC_DEST)),    
 Rtrim(Ltrim(T1.CO_BANC_CHEQ)),    
 Rtrim(Ltrim(T1.NU_CNTA_DEST)),    
 Rtrim(Ltrim(T1.TI_DOCU_DEST)),    
 Rtrim(Ltrim(T1.NU_DOCU_DEST)),    
 T1.CO_CAJA_DEST,    
 T1.CO_MONE_DEST, T1.FE_OPER_DEST,    
 T1.CO_TIPO_DOCU, T1.NU_DOCU,    
 T1.TI_ENTI_DEST,     
 T1.CO_ENTI_DEST,    
 T3.NO_AUXI,    
 T1.CO_TIPO_DEST,    
 T1.AA_DEST, T1.MM_DEST, T1.CO_UNID_DEST,     
 T1.FA_CAMB_DEST, T1.IM_MOVI, T1.IM_MOVI_DEST    
 From  TCMOVI_CAJA T1 , TTMONE T2 , TMAUXI_EMPR T3, TMCNTA_BANC T4, #TWDOCU_CART_LIQU T5    
 Where T1.CO_EMPR = @ISCO_EMPR    
 And T1.CO_CAJA = @ISCO_CAJA     
 And T1.AA_CAJA  = @ISAA_MOVI    
 And T1.MM_CAJA  = @ISMM_MOVI    
 And T1.TI_MOVI = 'I'    
 And T1.TI_SITU = 'CAR'    
 And T1.NU_CNTA_DEST Is Not Null    
 And T1.CO_MONE_DEST = T2.CO_MONE    
 And T1.CO_EMPR = T3.CO_EMPR    
 And T1.TI_ENTI_DEST = T3.TI_AUXI_EMPR    
 And T1.CO_ENTI_DEST = T3.CO_AUXI_EMPR    
 And T1.CO_EMPR = T4.CO_EMPR    
 And T1.CO_BANC_DEST =  T4.CO_BANC    
 And T1.NU_CNTA_DEST =  T4.NU_CNTA_BANC    
 And T1.CO_BANC_DEST = T5.CO_BANC_DEST    
 And T1.NU_CNTA_DEST = T5.NU_CNTA_DEST    
 And T1.TI_DOCU_DEST = T5.TI_DOCU_DEST    
 And T1.NU_DOCU_DEST = T5.NU_DOCU_DEST      
 And T5.FL_DEST = 'T'    
    
 And T1.CO_BANC_DEST =  @CSCO_BANC_DEST    
 And T1.NU_CNTA_DEST =  @CSNU_CNTA_DEST    
 AND T1.AA_DEST = @CSAA_DEST    
 AND T1.MM_DEST = @CSMM_DEST    
 AND T1.TI_DOCU_DEST = @CSTI_DOCU_DEST    
 AND T1.NU_DOCU_DEST = @CSNU_DOCU_DEST    
 --AND T1.TI_ENTI_DEST = @CSTI_ENTI_DEST    
 --AND T1.CO_ENTI_DEST = @CSCO_ENTI_DEST    
    
--***************************************    
BEGIN    
    
Open  CU_TCMOVI_CAJA_I07_2    
Fetch  CU_TCMOVI_CAJA_I07_2 Into     
 @CSFL_DEST_CUR2,    
 @CSCO_BANC_CUR2,    
 @CSCO_BACH_CUR2,    
 @CSNU_CNTA_CUR2,    
 @CSTI_DOCU_CUR2,    
 @CSNU_DOCU_CUR2,    
 @CSCO_CAJA_CUR2,    
 @CSCO_MONE_CUR2, @CDFE_OPER_CUR2,    
 @CSTI_DOCU_CUR2, @CSNU_DOCU_CUR2,    
 @CSTI_ENTI_CUR2,     
 @CSCO_ENTI_CUR2,    
 @CSNO_AUXI_CUR2,   @CSCO_TIPO_CUR2,    
 @CSAA_DEST_CUR2, @CSMM_DEST_CUR2, @CSCO_UNID_CUR2,    
 @CNFA_CAMB_CUR2, @CNIM_MOVI_CUR2, @CNIM_CONV_CUR2    
    
While (@@FETCH_STATUS = 0)    
 BEGIN --LOOP   
 If (@@FETCH_STATUS < 0)    
     Begin    
     Raiserror 20001 'FIN DE ARCHIVO'    
     Close CU_TCMOVI_CAJA_I07_2    
     Deallocate CU_TCMOVI_CAJA_I07_2    
     Close CU_TCMOVI_CAJA_I07_1    
     Deallocate CU_TCMOVI_CAJA_I07_1    
     Return    
    
      End    
    
 Begin     
    
 Select @VNNU_SECU = @VNNU_SECU + 1    
    
 Insert Into TDCHEQ_BANC (CO_EMPR, CO_BANC,CO_UNID_CONC, NU_CNTA_BANC, AA_BNCO,     
  MM_BNCO, TI_MOVI_BANC,  NU_COMP_BANC, NU_SECU, TI_ENTI,     
  CO_ENTI, CO_BANC_CHEQ, NU_CHEQ, CO_MONE,     
  IM_CHEQ, IM_CONV, FE_DISP, FE_DIFE, CO_USUA_CREA,FE_USUA_CREA, CO_USUA_MODI, FE_USUA_MODI)    
 Values(@ISCO_EMPR, @CSCO_BANC_CUR2,@CSCO_UNID_CUR2,@CSNU_CNTA_CUR2, @CSAA_DEST_CUR2,    
  @CSMM_DEST_CUR2, 'I', @VSNU_COMP_MOVI, @VNNU_SECU, @CSTI_ENTI_CUR2,    
  @CSCO_ENTI_CUR2, @CSCO_BACH_CUR2, @CSNU_DOCU_CUR2, @CSCO_MONE_CUR2,    
  @CNIM_MOVI_CUR2, @CNIM_CONV_CUR2, '', '', @ISCO_USUA,GETDATE(), @ISCO_USUA, GETDATE())    
    
     If @@error <> 0    
            Begin    
               Rollback tran    
   Close CU_TCMOVI_CAJA_I07_1    
   DEALLOCATE CU_TCMOVI_CAJA_I07_1    
   Close CU_TCMOVI_CAJA_I07_2    
   DEALLOCATE CU_TCMOVI_CAJA_I07_2    
         Return    
            End     
    
 Fetch  CU_TCMOVI_CAJA_I07_2 Into     
  @CSFL_DEST_CUR2,     
  @CSCO_BANC_CUR2,    
  @CSCO_BACH_CUR2,    
  @CSNU_CNTA_CUR2,    
  @CSTI_DOCU_CUR2,    
  @CSNU_DOCU_CUR2,    
  @CSCO_CAJA_CUR2,    
  @CSCO_MONE_CUR2, @CDFE_OPER_CUR2,    
  @CSTI_DOCU_CUR2, @CSNU_DOCU_CUR2,    
  @CSTI_ENTI_CUR2,     
  @CSCO_ENTI_CUR2,    
  @CSNO_AUXI_CUR2,    
  @CSCO_TIPO_CUR2,    
  @CSAA_DEST_CUR2, @CSMM_DEST_CUR2, @CSCO_UNID_CUR2,    
  @CNFA_CAMB_CUR2, @CNIM_MOVI_CUR2, @CNIM_CONV_CUR2    
 End     
    
END -- LOOP    
    
Close CU_TCMOVI_CAJA_I07_2    
DEALLOCATE CU_TCMOVI_CAJA_I07_2    
    
END    
    
  --- Actualiza Situacion del Origen     
  Begin     
   UPDATE  TCMOVI_CAJA    
   SET  TI_SITU = 'LIQ',    
    NU_COMP_DEST = @VSNU_COMP_MOVI,     
    CO_USUA_MODI = @ISCO_USUA,    
    FE_USUA_MODI = Getdate()    
   Where CO_EMPR = @ISCO_EMPR    
        And CO_CAJA = @ISCO_CAJA     
        And AA_CAJA  = @ISAA_MOVI            
    And MM_CAJA  = @ISMM_MOVI    
        And CO_BANC_DEST = @CSCO_BANC_DEST    
        And NU_CNTA_DEST = @CSNU_CNTA_DEST    
        And TI_DOCU_DEST = @CSTI_DOCU_DEST     
        And NU_DOCU_DEST = @CSNU_DOCU_DEST    
        And TI_MOVI = 'I'    
        And TI_SITU = 'CAR'    
    
      If @@error <> 0    
             Begin    
                Rollback tran    
    Close CU_TCMOVI_CAJA_I07_1    
    DEALLOCATE CU_TCMOVI_CAJA_I07_1    
    
          Return    
    
             End     
  End -- Fin  Actulizacion Origen     
    
    END ---FIN BANCOS    
    
    
    
 If @CSFL_DEST = 'C' ---Caja    
    BEGIN    
  Select  @VNNU_COMP_MOVI = IsNull(Max(Convert(Numeric,NU_COMP_CAJA)),0)    
  From  TCMOVI_CAJA     
  Where  CO_EMPR = @ISCO_EMPR     
    And CO_CAJA = @CSCO_CAJA_DEST     
     And AA_CAJA = @CSAA_DEST    
     And MM_CAJA = @CSMM_DEST     
     And TI_MOVI = 'I'    
    
  Select  @VSTI_CORR_INEG = TI_CORR_INEG     
--   @VSFR_CORR_INEG = FR_CORR_INEG,     
--   @VNUL_CORR_INGR = UL_CORR_INGR    
   From  TMCAJA    
   Where  CO_EMPR = @ISCO_EMPR    
     And RTRIM(CO_CAJA) = RTRIM( @CSCO_CAJA_DEST )    
    
    
  Select @VNNU_COMP_MOVI = @VNNU_COMP_MOVI + 1    
  Select @VSNU_COMP_MOVI = Replicate('0',8-LEN(Convert(Varchar,@VNNU_COMP_MOVI))) + Convert(Varchar,@VNNU_COMP_MOVI)    
    
  Begin     
    
    
      Exec SP_TCMOVI_CAJA_I06  @ISCO_EMPR, @ISCO_USUA,    
   @CSCO_CAJA_DEST, @CSAA_DEST, @CSMM_DEST,  'I', @CDFE_OPER_DEST ,     
   @CSCO_TIPO_DEST, @CSCO_SUBR_DEST, @CNFA_CAMB_DEST, @CNIM_MOVI_DEST, @CSTI_ENTI_DEST,     
   @CSCO_ENTI_DEST, @CSNO_AUXI , @CSDE_OBSE_DEST, @VSTI_CORR_INEG,      
   @VNUL_CORR_INGR, @VNUL_CORR_INEG, NULL, @CSCO_UNID_DEST, NULL, @VSST_ASTO_RESU,     
   @VSNU_COMP_MOVI OUTPUT    
    
     If @@error <> 0    
            Begin    
               Rollback tran    
   Close CU_TCMOVI_CAJA_I07_1    
   DEALLOCATE CU_TCMOVI_CAJA_I07_1    
    
         Return    
       End     
    
  End -- FIN INSERTA CABECERA CAJA.    
    
  --- Actualiza Situacion del Origen     
  Begin     
    
   UPDATE  TCMOVI_CAJA    
   SET  TI_SITU = 'LIQ',    
    NU_COMP_DEST = @VSNU_COMP_MOVI,     
    CO_USUA_MODI = @ISCO_USUA,    
    FE_USUA_MODI = Getdate()    
   Where CO_EMPR = @ISCO_EMPR    
        And CO_CAJA = @ISCO_CAJA     
        And AA_CAJA  = @ISAA_MOVI    
        And MM_CAJA  = @ISMM_MOVI    
        And CO_CAJA_DEST = @CSCO_CAJA_DEST    
        And TI_MOVI = 'I'    
        And TI_SITU = 'CAR'    
    
      If @@error <> 0    
             Begin    
                Rollback tran    
    Close CU_TCMOVI_CAJA_I07_1    
    DEALLOCATE CU_TCMOVI_CAJA_I07_1    
          Return    
           End     
  End -- Fin  Actulizacion Origen     
    
  END ---FIN CAJA    
Fetch  CU_TCMOVI_CAJA_I07_1 Into     
 @CSFL_DEST,    
 @CSCO_BANC_DEST,    
 @CSNU_CNTA_DEST,    
 @CSTI_DOCU_DEST,    
 @CSNU_DOCU_DEST,    
 @CSCO_CAJA_DEST ,    
 @CSCO_MONE_DEST , @CDFE_OPER_DEST,    
 @CSTI_DOCU_DEST , @CSNU_DOCU_DEST,    
 @CSTI_ENTI_DEST ,     
 @CSCO_ENTI_DEST ,    
 @CSNO_AUXI,    
 @CSCO_TIPO_DEST,    
 @CSAA_DEST  , @CSMM_DEST, @CSCO_UNID_DEST,    
 @CNFA_CAMB_DEST , @CNIM_MOVI_DEST, @CSDE_OBSE_DEST, @CSCO_SUBR_DEST     
 End --LOOP    
    
Close CU_TCMOVI_CAJA_I07_1    
DEALLOCATE CU_TCMOVI_CAJA_I07_1    
    
COMMIT TRAN    
    
End    
    
/*------------------------ Fin ------------------------------*/


GO
